# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name,field_selection_name as selection_name
from openerp.addons.dm_rubylong import get_rubylong_fields_xml,get_rublong_data_url
from openerp.osv import fields, osv

class mrp_rework(osv.Model):
    
    _inherit = 'mrp.rework'
    
    def _get_rubylong_xml(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        data_xml = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
                        #返工单
                        'name',
                        #产品
                        ('product_id.default_code','default_code'),
                        ('product_id.name','product_id'),
                        #当前工单
                        ('mo_source_id.name','mo_source_id'),
                        #当前工单工序
                        ('wo_source_id.code','code'),
                        ('wo_source_id.name','wo_source_id'),
                        #物料清单
                        ('bom_id.code','code_bom'),
                        ('bom_id.name','bom_id'),
                        #工艺路线
                        ('routing_id.name','routing_id'),
                        #返工生成的分工单
                        ('mo_sub_id.name','mo_sub_id'),
                        #申请数量
                        'qty_request',
                        #返工数量
                        'qty',
                        #报废数量
                        'qty_scrap',
                        #返工起始工序
                        ('oper_start_id.name','oper_start_id'),
                        #相关返工工单工序
                        ('wo_target_id.code','wo_code'),
                        ('wo_target_id.name','wo_target_id'),
                        #相关返工工单
                        ('mo_target_id.name','mo_target_id'),
                        #报废原因
                        ('scarp_reason_id.name','scarp_reason_id'),
                        #报废说明
                        'scarp_note',
                        #报废单
                        ('scrap_order_id.name','scrap_order_id'),
                        #报废库位
                        ('scrap_loc_id.name','scrap_loc_id'),
                        #备注
                        'note',
                        ]
            #良品数量【申请-返工-报废=良品】
            qty_ok = order.qty_request - order.qty -order.qty_scrap
            order_fields.append({'qty_ok':qty_ok})
            
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)

        return res            

    _columns = {
            'rubylong_xml_file':fields.function(_get_rubylong_xml, type='text', string='Rubylong xml'),
            'company_id':fields.many2one('res.company','Company'),
            }
    _defaults = {
        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'mrp.rework', context=c),
    }
    
    
#po_super.STATE_SELECTION = STATE_SELECTION_PO    